//
//  BHMyCompetitionChildViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/17.
//

import UIKit

class BHMyCompetitionChildViewController: BHBaseViewController {
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.sectionInset = .zero
        let view = UICollectionView.buildCollectionView(layout: layout)
        view.register(UINib.init(nibName: "BHWonderfulGameCell", bundle: nil), forCellWithReuseIdentifier: "BHWonderfulGameCell")
        view.backgroundColor = .white
        view.delegate = self
        view.dataSource = self
        
        return view
    }()

    var childType: String = ""
    
    fileprivate var gameArray: [BHCommonGameModel] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.removeFromSuperview()
       
        self.view.addSubview(self.collectionView)
        
        self.collectionView.addRefresh { [weak self] in
            self?.page = 1
            self?.queryGameListService()
        }
        
        self.collectionView.addloadMore { [weak self] in
            self?.page += 1
            self?.queryGameListService()
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.collectionView.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: self.view.bh_height)
    }

    func refreshChildSouceService()  {
        if self.gameArray.count == 0 {
            self.page = 1
            self.queryGameListService()
        }
    }

}
extension BHMyCompetitionChildViewController: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return self.gameArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: UIScreen.width, height: BHWonderfulGameCell.gameHeight)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.gameArray[indexPath.row]
        let detailVC = BHMyCompetitionDetailViewController.init()
        detailVC.applyId = model.gameId
        BHNavigationController.currentNavigationController?.pushViewController(detailVC, animated: true)
    }
    
}

extension BHMyCompetitionChildViewController: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "BHWonderfulGameCell", for: indexPath) as! BHWonderfulGameCell
        let model = self.gameArray[indexPath.row]
        cell.model = model
        return cell
    }
    
    
}


extension BHMyCompetitionChildViewController {
    func queryGameListService()  {
        if self.page == 1 {
            SVProgressHUD.show()
        }
        let manager = BHHTTPClient.init()
        manager.get(url: String.me_myGameState_list_url, paramters: ["pageNo":self.page,"pageSize":"10","type":self.childType]) { (success, errorMsg, responseDic) in
            if self.page == 1 {
                SVProgressHUD.dismiss()
            }
            self.collectionView.endRefresh()
            self.collectionView.endLoading()
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                let total = BHJsonTool.decodeIntFromDic(dic: result, key: "total")

                if self.page == 1 {
                    
                    self.collectionView.endLoadWithNoMoreData(false)
                    if self.gameArray.count != 0 {
                        self.gameArray.removeAll()
                    }
                }
                
                let record = BHJsonTool.decodeArrayFromDic(dic: result, key: "records")
                for dic in record {
                    let model = BHCommonGameModel.init()
                    model.parseDic(dic as! [String : Any])
                    self.gameArray.append(model)
                }
                self.collectionView.reloadData()
                if self.gameArray.count >= total {
                    self.collectionView.endLoadWithNoMoreData(true)
                }
                
            }else {
                BHToast.toast(text: errorMsg)
                if self.page != 1 {
                    self.page -= 1
                }
            }
        }
    }
}
